﻿// OssXmlTest.cpp : 定义控制台应用程序的入口点。
//

#include "OssXml.h"
#include <ossTime.h>

/**
 *      测试新建xml文件
 */
void TestNewXml()
{
    HXmlDoc hDoc = OssXmlNewDoc();
    if (hDoc == NULL)
    {
        printf("新建xmldoc失败\n");
    }

    HXmlNode hWjhNode = OssXmlNewNode("wjh");
    if (hWjhNode == NULL)
    {
        printf("创建wjh节点失败\n");
    }

    HXmlAttr hWjhProp = OssXmlNewProp(hWjhNode,"wife","lumei");
    if (hWjhProp == NULL)
    {
        printf("创建wjh的属性失败\n");
    }

    HXmlNode hWjhChild = OssXmlNewChild(hWjhNode,"daughter","wky");
    if (hWjhChild == NULL)
    {
        printf("创建wjh的子节点失败\n");
    }
    
    OssXmlSetRootNode(hDoc,hWjhNode);

    HXmlNode hLumeiNode = OssXmlNewNode("lumei");
    HXmlAttr hLumeiProp = OssXmlNewProp(hLumeiNode,"husband","wjh");

    HXmlNode hLumeiNode2 = OssXmlNewNode("lumei");
    HXmlAttr hLumeiProp2 = OssXmlNewProp(hLumeiNode2,"daughter","wky");

    OssXmlAddChild(hWjhNode,hLumeiNode);
    OssXmlAddChild(hWjhNode,hLumeiNode2);

    OssXmlSetRootNode(hDoc,hWjhNode);

    OssXmlSave("wjhlumei.xml",hDoc);

    HXmlNode curNode = OssXmlFirstChildNode(hWjhNode);
    while (curNode)
    {
        char *nodeName = OssXmlGetNodeName(curNode);
        printf("<%s ",nodeName);

        if (OssXmlHasProp(curNode,"husband"))
        {
            const char *attrValue = OssXmlGetPropValue(curNode);
            if (attrValue)
            {
                printf("%s",attrValue);
            }
        }
        printf(">\n");

        HXmlNode childNode = OssXmlFirstChildNode(curNode);
        while (childNode)
        {
            nodeName = OssXmlGetNodeName(childNode);
            printf("\t<%s ",nodeName);

            HXmlAttr hattr = OssXmlHasProp(childNode, "husband");
            if (hattr)
            {
                const char *attrValue = OssXmlGetPropValue(hattr);
                if (attrValue)
                {
                    printf("%s",attrValue);
                }
            }
            printf(">\n");
            childNode = OssXmlNextBrotherNode(childNode);
        }
        curNode = OssXmlNextBrotherNode(curNode);
    }


    OssXmlFreeDoc(hDoc);
}

void TestFindNode()
{
    HXmlDoc hDoc = OssXmlParseFile("D:\\testXml.xml");
    if (hDoc == NULL)
    {
        printf("打开textXml.xml失败\n");
        return ;
    }

    HXmlNode hNode = OssXmlFindChildNode(OssXmlGetRootNode(hDoc),"driver");
    if (hNode == NULL)
    {
        printf("没有找到driver节点\n");
        return ;
    }

    hNode = OssXmlFindChildNode(OssXmlGetRootNode(hDoc),"param");
    if (hNode == NULL)
    {
        printf("没有找到param节点\n");
        return ;
    }
    else
    {
        printf("param:%s\n",OssXmlGetNodeContent(hNode));
    }
}

void TestParseXml()
{
   OssXml xml;

   HXmlDoc hDoc = xml.ParseFile("D:\\TelnetDeviced.dev");

   do 
   {
       HXmlNode hRootNode = xml.GetRootNode(hDoc);
       if (hRootNode == NULL)
       {
           break;
       }

       HXmlNode hDriver = xml.FindChildNode(hRootNode,(char*)"driver");
       if (hDriver == NULL)
       {
           break;
       }

       HXmlNode hDriverPath = xml.FindChildNode(hDriver,(char*)"path");
       if (hDriverPath == NULL)
       {
           break;
       }

       const char* content = xml.GetNodeContent(hDriverPath);

       HXmlNode hParamNode = xml.FindChildNode(hRootNode,(char*)"param");
       if (hParamNode)
       {
           const char* pcParam = xml.GetNodeContent(hParamNode);
           printf("\npcParam=%s\n",pcParam);
       }
   } while (FALSE);
   xml.FreeDoc(hDoc);
}
int main(int argc, char* argv[])
{
    TestParseXml();
    //TestNewXml();
    //TestFindNode();
#if 0
    DWord st;

    st = OssGetTickCount();

    for (int i=0;i<1;i++)
    {
        TestNewXml();
    }

    printf("测试完成,耗时%dms\n",OssGetTickCount() - st);
#endif
    //getchar();
	return 0;
}

